/**
 * 设备BOM维护 - 编辑页面
 * @author shixc
 * @date 2018/01/17
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            form: '',
            rules: {
                MMRB001: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRB002: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRA004: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRB003: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRB004: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRB005: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRB006: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                /*MMRB020: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],*/
            },
            selectOptions: {
                options1: [],
                options2: [],
                options3: []
            }
        },
        callObj,
        pageVue
    ;

    var Gwai620Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai620_edit',
                _loadTplsFull: [
                {
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }
                ]
            }, win);
        },
        edit: function () {
            app.ajax({
                xurl: 'crm/gwai620/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: '设备BOM信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        //callObj.update(result.xdata);
                        app.pageModule('gwai620').reloadTableCurr();
                        layer.close(callObj.layOpenIndex);
                    }
                }
            });
        },
        add: function () {
            app.ajax({
                xurl: 'crm/gwai620/add',
                xdata: vueData.form,
                loading: true,
                okmsg: '设备BOM信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwai620').reloadTableCurr();
                        layer.close(callObj.layOpenIndex);
                    }
                }
            });
        },
        _call: function (obj) {
            pageVue.$refs['form'].resetFields();
            callObj = obj;
            if (obj.is_add) {
                vueData.isAdd = true;
                app.extendUpLow({
                    target: vueData.form,
                    res: {},
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
            }

        },
        _ready: function () {
            vueData.form = app.fields_(['MMRB']);
            pageVue = new Vue({
                el: '#gwai620_edit',
                data: vueData,
                methods: {
                    cancel: function () {
                        layer.close(callObj.layOpenIndex);
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.add();
                                } else {
                                    self.edit();
                                }
                            }
                        });
                    },
                    open: function (item) {
                        app.ajax({
                            xurl: 'crm/gwai620/getMmraList',
                            xdata: {MMRA001: vueData.form.MMRB001},
                            cb: function (result) {
                                vueData.selectOptions.options2 = result.data.list;
                            }
                        });
                    },
                    openKh:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'cq_gwak001_4',
                                    columnList: [
                                        {label: '客户编号', prop: 'GWAK001', width: '150'},
                                        {label: '客户名称', prop: 'GWAKL004', width: '150'}
                                    ]
                                },
                                cb:function (data) {
                                    vueData.form.MMRB001 = data.GWAK001;
                                }
                            },
                            index:0,
                            openOpts: {
                                title: '选择客户',
                                area:['600px', '470px']
                            }
                        });
                    },
                    openMmra002:function () {
                        if(vueData.form.MMRB001 == '' || vueData.form.MMRB001 == null){
                            app.showWarning("请先选择客户编号！");
                            return;
                        }
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config:{
                                    method:'cq_mmra001_1',
                                    columnList: [
                                        {label: '客户编号', prop: 'MMRA001', width: '150'},
                                        {label: '设备编号', prop: 'MMRA002', width: '150'},
                                        {label: '设备名称', prop: 'MMRA004', width: '150'},
                                        {label: '规格', prop: 'MMRA006', width: '150'},
                                        {label: '序列号', prop: 'MMRA003', width: '150'}
                                    ],
                                    searchParam:{
                                        MMRA001:vueData.form.MMRB001,
                                    }
                                },
                                cb:function (data) {
                                    vueData.form.MMRB002 = data.MMRA002;
                                    vueData.form.MMRA004 = data.MMRA004;
                                    vueData.form.MMRB003 = data.MMRA003;
                                    vueData.form.MMRA006 = data.MMRA006;
                                    vueData.form.MMRB010 = data.MMRA008;
                                    vueData.form.MMRB011 = data.MMRA012.substr(0,10);
                                }
                            },
                            index:0,
                            openOpts: {
                                title: '选择设备',
                                area:['600px', '470px']
                            }
                        });
                    },
                    clearMmrb002:function() {
                        vueData.form.MMRA004 = '';
                        vueData.form.MMRB003 = '';
                        vueData.form.MMRA006 = '';
                        vueData.form.MMRB010 = '';
                        vueData.form.MMRB011 = '';
                    },
                    openMmra004:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'cq_gwam001_2',
                                    columnList: [
                                        {label: '配件编号', prop: 'GWAM001', width: '150'},
                                        {label: '品名', prop: 'GWAML003', width: '150'},
                                        {label: '规格', prop: 'GWAML004', width: '150'}
                                    ]
                                },
                                cb:function (data) {
                                    vueData.form.MMRB004 = data.GWAM001;
                                    vueData.form.MMRB007 = data.GWAML004;
                                    vueData.form.MMRB006 = data.GWAML003;
                                    vueData.form.MMRB012 = data.GWAM002;
                                    vueData.form.MMRB005 = data.GWAM004;
                                }
                            },
                            index:0,
                            openOpts: {
                                title: '选择配件',
                                area:['600px', '470px']
                            }
                        });
                    },
                    clearMmrb004:function () {
                        vueData.form.MMRB007 = '';
                        vueData.form.MMRB006 = '';
                        vueData.form.MMRB012 = '';
                        vueData.form.MMRB005 = '';
                    }
                },
                created: function () {
                    self._setEleIsReady('gwai620_edit');
                }
            });
        },
        _lazy: function () {
            /*app.ajax({
                xurl: 'crm/gwai620/getGwakList',
                cb: function (result) {
                    vueData.selectOptions.options1 = result.data.list;
                }
            });*/
            /*app.ajax({
                xurl: 'crm/gwai620/getMmraList',
                xdata: {MMRA001: vueData.form.MMRB001},
                cb: function (result) {
                    vueData.selectOptions.options2 = result.data.list;
                }
            });*/
            /*app.ajax({
                xurl: 'crm/gwai620/getGwamList',
                cb: function (result) {
                    vueData.selectOptions.options3 = result.data.list;
                }
            });*/
            this._super();
        }
    });

    return new Gwai620Edit();

});
